package com.goodomen.garbage.web.util;

import java.security.SecureRandom;


public class RandomStringUtils {

    private static final char[] PRINTABLE_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345679"
            .toCharArray();

    private static final int DEFAULT_MAX_RANDOM_LENGTH = 35;

    public static String getNewString() {
        byte[] random = getNewStringAsBytes();

        return convertBytesToString(random);
    }

    private static byte[] getNewStringAsBytes() {
        final byte[] random = new byte[DEFAULT_MAX_RANDOM_LENGTH];

        new SecureRandom().nextBytes(random);

        return random;
    }

    private static String convertBytesToString(final byte[] random) {
        final char[] output = new char[random.length];
        for (int i = 0; i < random.length; i++) {
            final int index = Math.abs(random[i] % PRINTABLE_CHARACTERS.length);
            output[i] = PRINTABLE_CHARACTERS[index];
        }
        return new String(output);
    }
}
